package cn.myspringboot.system.controller;

import cn.myspringboot.anno.PreAuthorize;
import cn.myspringboot.system.service.MenuService;
import cn.myspringboot.user.domain.Menu;
import cn.myspringboot.user.query.MenuQuery;
import cn.myspringboot.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api("菜单相关接口")
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private MenuService menuService;

    @PreAuthorize(name = "添加或修改菜单信息",sn = "menu:add",descs ="添加或修改菜单信息")
    @ApiOperation("添加或修改菜单信息")
    @PostMapping
    public R insert(@RequestBody Menu menu) {
        if(menu.getId() == null){
            menuService.add(menu);
        }else {
            menuService.update(menu);
        }
        return R.ok();
    }
    @PreAuthorize(name = "根据id删除菜单信息",sn = "menu:delete",descs ="根据id删除菜单信息")
    @ApiOperation("根据id删除菜单信息")
    @DeleteMapping("/{id}")
    public R delete(@PathVariable("id") Long id) {
        menuService.delete(id);
        return R.ok();
    }
    @PreAuthorize(name = "批量删除菜单信息",sn = "menu:delBatch",descs ="批量删除菜单信息")
    @ApiOperation("批量删除菜单信息")
    @PostMapping("/delBatch")
    public R delBatch(@RequestBody List<Long> ids) {
        menuService.batchDelete(ids);
        return R.ok();
    }
    @PreAuthorize(name = "根据id查询菜单信息",sn = "menu:getById",descs ="根据id查询菜单信息")
    @ApiOperation("根据id查询菜单信息")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(menuService.getById(id));
    }
    @PreAuthorize(name = "查询所有菜单信息",sn = "menu:getAll",descs ="查询所有菜单信息")
    @ApiOperation("查询所有菜单信息")
    @GetMapping("/getAll")
    public R getAll() {
        return R.ok(menuService.getAll());
    }
    @PreAuthorize(name = "分页查询菜单信息",sn = "menu:page",descs ="分页查询菜单信息")
    @ApiOperation("分页查询菜单信息")
    @PostMapping("/page")
    public R page(@RequestBody MenuQuery menuQuery) {
        return R.ok(menuService.queryPage(menuQuery));
    }

    @PreAuthorize(name = "查询菜单中所有类型",sn = "menu:getByNameTree",descs ="查询菜单中所有类型")
    @ApiOperation("查询菜单中所有类型")
    @GetMapping("/getByNameTree")
    public R getByNameTree() {
        return R.ok(menuService.getByNameTree());
    }
}
